package com.rain.shoppingscienceproj.ui.activity.message;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.http.BaseSubscriber;
import com.rain.shoppingscienceproj.http.ExceptionHandle;
import com.rain.shoppingscienceproj.http.RetrofitClient;
import com.rain.shoppingscienceproj.module.Massage;
import com.rain.shoppingscienceproj.ui.activity.BaseActivity;
import com.rain.shoppingscienceproj.ui.activity.login.LoginActivity;
import com.rain.shoppingscienceproj.ui.adapter.MessageStoreAdapter;
import com.rain.shoppingscienceproj.ui.base.GlobalData;
import com.rain.shoppingscienceproj.ui.utile.TitleUtils;
import com.rain.shoppingscienceproj.utils.AppManager;
import com.rain.shoppingscienceproj.utils.LoadingDialog;
import com.rain.shoppingscienceproj.utils.rxBus.RxBus;
import com.rain.shoppingscienceproj.utils.rxBus.RxBusSubscriber;
import com.rain.shoppingscienceproj.utils.rxBus.RxSubscriptions;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MessageActivity extends BaseActivity {


    @BindView(R.id.rv_data)
    RecyclerView mRecyclerView;
    private MessageStoreAdapter adapter;

    @BindView(R.id.ll_no_data)
    LinearLayout ll_no_data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        TitleUtils.TitleView(this, "我的消息");
        userInfo = GlobalData.getUserInfo();
        loadingDialog = new LoadingDialog(this, "正在加载...");
        RxSubscriptions.add(RxBus.getInstance().toObserverable(String.class).subscribe(new RxBusSubscriber<String>() {
            @Override
            protected void onEvent(String s) {
                if (s.equals("MessageService")) {
                    initData();
                }
            }
        }));
        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxSubscriptions.isUnsubscribed();
        AppManager.getAppManager().removeActivity(this);
    }

    private void initData() {
        Map<String, String> map = new HashMap<>();
//         map.put("userId", userInfo.getUserId());
        map.put("token", userInfo.getToken());
        map.put("page", "1");
        loadingDialog.show();
        RetrofitClient.getInstance(this)
                .createBaseApi()
                .messageList(map, new BaseSubscriber<List<Massage>>(this) {
                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable e) {
//                        ToastUtils.showShortToast(e.Message);
                        if (e.Message.contains("token")) {
                            startActivitys(LoginActivity.class, 1);
                        }
                        loadingDialog.dismiss();
                        ll_no_data.setVisibility(View.VISIBLE);
                        mRecyclerView.setVisibility(View.GONE);
                    }

                    @Override
                    public void onNext(List<Massage> massages) {
                        super.onNext(massages);
                        if (massages.size() == 0) {
                            ll_no_data.setVisibility(View.VISIBLE);
                            mRecyclerView.setVisibility(View.GONE);
                        } else {
                            ll_no_data.setVisibility(View.GONE);
                            mRecyclerView.setVisibility(View.VISIBLE);
                        }
                        loadingDialog.dismiss();
                        adapter.setList(massages);

                    }
                });
    }

    private void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MessageStoreAdapter(this);
        mRecyclerView.setAdapter(adapter);
    }
}
